// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WorkCollection.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Defines the WorkCollection type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using AzureContrib.WindowsAzure.MEF;

namespace AzureContrib.WindowsAzure.WorkServices
{
	internal class WorkCollection : LazyValueMetadataCollection<IWorkItem, IWorkItemMetadata, IWork>, IWorkCollection
	{
		public override Lazy<IWork> Compact(Lazy<IWorkItem, IWorkItemMetadata> lazyValueMetadata)
		{
			return new Lazy<IWork>(() => new WorkWrapper(lazyValueMetadata));
		}

		internal class WorkWrapper : IWork
		{
			private readonly Lazy<IWorkItem, IWorkItemMetadata> lazyWorkItem;

			public WorkWrapper(Lazy<IWorkItem, IWorkItemMetadata> lazyWorkItem)
			{
				this.lazyWorkItem = lazyWorkItem;
			}

			public bool HasWork
			{
				get { return lazyWorkItem.Value.HasWork; }
			}

			public int Order
			{
				get { return lazyWorkItem.Metadata.Order; }
			}

			public WorkType WorkType
			{
				get { return lazyWorkItem.Metadata.WorkType; }
			}

			public string Name
			{
				get { return lazyWorkItem.Metadata.Name; }
			}

			public string Description
			{
				get { return lazyWorkItem.Metadata.Description; }
			}

			public void DoWork()
			{
				lazyWorkItem.Value.DoWork();
			}
		}
	}
}